package com.lwlean.test.utils;

import org.apache.commons.lang.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lean on 16-12-16.
 */
public class ValidUtil {

    /**
     * 根据传入的result返回校验信息
     * @param result
     * @return
     */
    public static String valid(BindingResult result){
        if(result.hasErrors()){
            final List<ObjectError> allErrors = result.getAllErrors();
            final List<String> messages = new ArrayList<>();
            allErrors.forEach(objectError -> {
                String defaultMessage = objectError.getDefaultMessage();
                messages.add(defaultMessage);
            });
            return StringUtils.join(messages, ",");
        }
        return null;
    }

}
